/*
 * LanguageVariablesView.java
 *
 * Created on 11 Kasım 2008 Salı, 12:56
 */
package org.testis.gui.mvc.view.testdesign;

import org.testis.gui.mvc.view.*;
import org.testis.gui.mvc.controller.testdesign.LanguageVariablesController;
import org.testis.gui.mvc.controller.TestDesignControllerList;
import org.testis.gui.mvc.utilities.AtofTableModel;
import org.testis.gui.mvc.utilities.ParameterTableModel;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author  nurhat
 */
public class LanguageVariablesView extends AtofBaseDialog implements
        AbstractViewPanel {

    private static final long serialVersionUID = 423874L;
    private String m_title = "Select any language variable to use";
    private TableRowSorter<TableModel> sorter;

    /** Creates new form LanguageVariablesView */
    public LanguageVariablesView(java.awt.Frame parent,
            boolean modal) {
        super(parent,
                modal);
        initComponents();

    }

    public LanguageVariablesView(Frame parent,
            boolean modal,
            ParameterTableModel tableModel) {
        super(parent,
                modal);
        initComponents();
        sorter = new TableRowSorter<TableModel>(tableModel);
        langTable.setRowSorter(sorter);
        langTable.setModel(tableModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        langTable = new javax.swing.JTable();
        btnExit = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtFilter = new javax.swing.JTextField();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(m_title);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel2.setBackground(new java.awt.Color(223, 219, 219));

        langTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        langTable.setUpdateSelectionOnSort(false);
        langTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                langTableMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(langTable);

        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        jLabel1.setText("Filter:");

        txtFilter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtFilterActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnExit)
                .addGap(30, 30, 30))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtFilter, javax.swing.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 527, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 461, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnExit)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
    this.setVisible(false);
    this.setEnabled(false);
    this.dispose();
}//GEN-LAST:event_btnExitActionPerformed

private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    WindowListener[] listeners = getWindowListeners();
    for (int i = 0; i < listeners.length; i++) {
        this.removeWindowListener(listeners[i]);
    }
    this.setVisible(false);
    this.setEnabled(false);
    TestDesignControllerList.newInstance().getLanguageVariablesController().
            operationCloseWindow();
    this.dispose();
}//GEN-LAST:event_formWindowClosed

private void langTableMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_langTableMousePressed
    Point start = evt.getPoint();
    langTable.changeSelection(langTable.rowAtPoint(start),
            0,
            false,
            false);
    int rowIndex = langTable.getSelectedRow();
    System.out.println("");


    int startingRow =
            langTable.getSelectedRow();
    String currentParameter =
            (String) langTable.getValueAt(
            rowIndex,
            0);

    String currentValue =
            (String) langTable.getValueAt(rowIndex,
            1);
    TestDesignControllerList.newInstance().getLanguageVariablesController().
            operationChangeLanguageVariable(currentParameter);
}//GEN-LAST:event_langTableMousePressed

private void txtFilterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtFilterActionPerformed
    String text = txtFilter.getText();
    if (text.length() == 0) {
        sorter.setRowFilter(null);
    } else {
        sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
    }
}//GEN-LAST:event_txtFilterActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable langTable;
    private javax.swing.JTextField txtFilter;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                LanguageVariablesController.SHOW_VIEW)) {
            Boolean val = (Boolean) evt.getNewValue();
            this.centerParent();
        } else if (evt.getPropertyName().equals(
                LanguageVariablesController.SHOW_ERROR)) {
            this.setVisible(false);
            String val = (String) evt.getNewValue();
            JOptionPane.showMessageDialog(
                    this,
                    val,
                    "Error Dialog",
                    JOptionPane.ERROR_MESSAGE);

            this.setEnabled(false);
            this.dispose();
        } else if (evt.getPropertyName().equals(
                LanguageVariablesController.PARAMETER_MODEL_CHANGED)) {
            langTable.setModel((AtofTableModel) evt.getNewValue());
            sorter = new TableRowSorter<TableModel>((AtofTableModel) evt.getNewValue());
            langTable.setRowSorter(sorter);
            langTable.updateUI();
        }
    }
}
